/*
 * Copyright (C) 2010-2010 MaoJian Wu
 *
 * Author:
 *	MaoJian Wu
 *
 * Source:
 *	
 *
 * This file is a part of zfspear library.
 *
 *
 */

#include "jid.h"


Jid::Jid(const QString& domain) : domain_(domain) {
}

Jid::Jid(const QString& node,const QString& domain) : node_(node), domain_(domain) {
}

Jid::Jid(const QString& node,const QString& domain,const QString& resource) :
                node_(node), domain_(domain), resource_(resource) {
}

Jid::Jid(const Jid& other) : node_(other.node_),
                domain_(other.domain_), resource_(other.resource_) {
}

void Jid::SetNode(const QString& node) {
        node_ = node;
}

QString Jid::Node() const {
        return node_;
}

void Jid::SetDomain(const QString& domain) {
        domain_ = domain;
}

QString Jid::Domain() const {
        return domain_;
}

void Jid::SetResource(const QString& resource) {
        resource_ = resource;
}

QString Jid::Resource() const {
        return resource_;
}

QString Jid::ToString() const {
        Q_ASSERT(domain_.size() > 0);

        QString string;
        if (node_.size() > 0)
                string.append(node_).append("@");

        string.append(domain_);

        if (resource_.size() > 0) {
                string.append("/").append(resource_);
        }

        return string;
}

QString Jid::BareId() const {
        if (node_ == "")
                return domain_;

        return node_ + "@" + domain_;
}

bool Jid::FromString(const QString& jidString) {
        if (jidString.length() == 0)
                return false;

        QChar at('@');
        int atIndex = jidString.indexOf(at);
        if (atIndex == -1) {
                domain_ = jidString;
        } else {
                node_ = jidString.left(atIndex);
                QString domainAndResource = jidString.mid(atIndex + 1);

                // two '@' chars
                if (domainAndResource.indexOf(at) != -1)
                        return false;

                QChar slash = QChar('/');
                int slashIndex = domainAndResource.indexOf(slash);
                if (slashIndex == -1) {
                        domain_ = domainAndResource;
                } else {
                        domain_ = domainAndResource.left(slashIndex);
                        resource_ = domainAndResource.mid(slashIndex + 1);

                        // two '/' chars
                        if (resource_.indexOf(slash) != -1)
                                return false;
                }
        }

        if (domain_.length() == 0 || domain_.length() > 1023)
                return false;

        if (node_.length() > 1023 || resource_.length() > 1023)
                return false;

        return true;
}

bool Jid::operator==(const Jid& other) {
        return (node_ == other.Node() && domain_ == other.Domain() && resource_ == other.Resource());
}

bool Jid::operator!=(const Jid& other) {
        return !(*this == other);
}
